#include<stdio.h>
#include<string.h>

#define MAX 60
struct student{
	char name[20];
	int age;
	int id;
	char sex[10];
};

struct class_room{
	struct student st[MAX];
	int n;
};

void printf_menu()
{
	printf("        student manage system        \n");
	printf("-------------------------------------\n");
	printf("|1. show student info                |\n");
	printf("|2. add student info                 |\n");
	printf("|3. del student info                 |\n");
	printf("|4. fix student info                 |\n");
	printf("|5. find student info                |\n");
	printf("|6. exit                             |\n");
	printf("-------------------------------------\n");
}

void add_student(struct class_room *WLW)
{
	printf("please enter student name: \n");
	scanf("%s",WLW->st[WLW->n].name);
	printf("please enter student age: \n");
	scanf("%d",&WLW->st[WLW->n].age);
	printf("please enter student id: \n");
	scanf("%d",&WLW->st[WLW->n].id);
	printf("please enter student sex: \n");
	scanf("%s",WLW->st[WLW->n].sex);

	WLW->n++;
}

int show_student(struct class_room *WLW)
{
	int i;

	if (WLW->n == 0) {
		printf("Now no students, please add student. \n");
		return -1;
	}
	
	printf("The student table: \n");
	printf("name \t age \t id \t sex \n");
	for (i = 0; i < WLW->n; i++) {
		printf("%s \t %d \t %d \t %s \n", WLW->st[i].name, WLW->st[i].age, WLW->st[i].id, WLW->st[i].sex);
	}

	return 0;
}

int find_student(struct class_room *WLW)
{
	int id, i;
	printf("Please enter you want to find the student id: \n");
	scanf("%d", &id);

	for (i = 0; i < WLW->n; i++) {
		if (id == WLW->st[i].id) {
			printf("the student is exist!\n");
			printf("the %d student name is %s\n",i+1,WLW->st[i].name);
			printf("the %d student age is %d\n",i+1,WLW->st[i].age);
			printf("the %d student id is %d\n",i+1,WLW->st[i].id);
			printf("the %d student sex is %s\n",i+1,WLW->st[i].sex);
			return i;
		}
	}
	printf("the student is not exist!\n");
	return -1;
}

void remove_student(struct class_room *WLW)
{
	int ret, i;
	ret = find_student(WLW);

	if(ret!=-1) {
		for (i = ret; i < WLW->n - 1; i++) {
			strcpy(WLW->st[i].name,WLW->st[i+1].name);
			WLW->st[i].age=WLW->st[i+1].age;
			WLW->st[i].id=WLW->st[i+1].id;
			strcpy(WLW->st[i].sex,WLW->st[i+1].sex);
		}

		WLW->n--;
	}
	printf("Del this student info success! \n");
}

void change_student(struct class_room *WLW)
{
	int ret, choose;
	ret = find_student(WLW);

	if (ret != -1) {
		printf("Fix student info which?\n");
		printf("1. name\n");
		printf("2. age\n");
		printf("3. id\n");
		printf("4. sex\n");
		scanf("%d", &choose);

		switch(choose) {
			case 1:
				printf("please enter student new name: \n");
				scanf("%s",WLW->st[ret].name);
				break;
			case 2:
				printf("please enter student new age: \n");
				scanf("%d",&WLW->st[ret].age);
				break;
			case 3:
				printf("please enter student new id: \n");
				scanf("%d",&WLW->st[ret].id);
				break;
			case 4: 
				printf("please enter student new sex: \n");
				scanf("%s",WLW->st[ret].sex);
				break;
			default:
				printf("enter error, please try again! \n");
		}
	}
}

int main() 
{
	struct class_room WLW;
	WLW.n=0;
	int choose;

	printf_menu();

	while(1) {
		printf("please enter num to chose function:  \n");
		scanf("%d", &choose);

		switch(choose) {
			case 1:
				show_student(&WLW);
				break;
			case 2:
				add_student(&WLW);
				break;
			case 3:
				remove_student(&WLW);
				break;
			case 4:
				change_student(&WLW);
				break; 
			case 5:
				find_student(&WLW);
				break;
			case 6:
				return 0;
			default:
				printf("enter error, please try again! \n");
		}
	}
}

